package com.daidai.activitys

import android.graphics.PixelFormat
import android.os.Handler
import android.provider.Settings
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.daidai.base.BaseActivity
import com.daidai.spinner.R
import com.daidai.tools.G

class ViewPosActivity : BaseActivity() {

    lateinit var tv: TextView
    var winView:View? = null

    override fun getContentViewResId(): Int = R.layout.activity_viewpos

    override fun initView() {
        tv = findViewById(R.id.tv)

        // 1s 后打印 tv的坐标信息
        Handler().postDelayed({
            //
            printInfo(tv)

            // 设置下 x.y 在打印一次
            tv.x += 100
            tv.y += 100
            Handler().postDelayed({
                G.elog("移动后:")
                printInfo(tv)
            }, 2000)

        }, 2000)


        // btn
        findBtn(R.id.btn).setOnClickListener {
            if (Settings.canDrawOverlays(this)){
                showWindow()
            }
        }
    }
    private fun showWindow(){
        if (winView != null){
            return
        }

        // 1. view
        winView = TextView(this).apply {
            setBackgroundResource(R.color.black)
            text = "I'm window!XXXX"
            setTextColor(0xFFFFFF)
        }

        val params = WindowManager.LayoutParams()
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.x = 400
        params.y = 600
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        params.format = PixelFormat.TRANSPARENT
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        params.gravity = Gravity.LEFT or Gravity.TOP
        windowManager.addView(winView, params)

        // 1s后打印
        // 1s 后打印 tv的坐标信息
        Handler().postDelayed({
            //
            printInfo(winView!!)

            // 设置下 x.y 在打印一次
            winView!!.x += 50
            winView!!.y += 10
            winView!!.invalidate()
            Handler().postDelayed({
                G.elog("移动后:")
                printInfo(winView!!)
            }, 2000)

        }, 2000)
    }

    override fun initData() {
    }

    private fun printInfo(view:View) {
        G.elog("=================================\n当前信息:")
        G.elog("x:${view.x},y:${view.y}")
        G.elog("left:${view.left},top:${view.top}")
        val inWindow = intArrayOf(0, 0)
        val inScreen = intArrayOf(0, 0)
        view.getLocationInWindow(inWindow)
        view.getLocationOnScreen(inScreen)
        G.elog("inWindow:${inWindow[0]},${inWindow[1]}")
        G.elog("inScreen:${inScreen[0]},${inScreen[1]}")
    }


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        G.elog("Activity->dispatchTouchEvent")
        G.elog("action:${ev?.let { it.action }}")
        val retValue =  super.dispatchTouchEvent(ev)
        G.elog("retValue:$retValue}")
        return retValue
    }
}
